package com.khalil.kkadmin.web;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.khalil.kkadmin.common.util.StrUtil;
import com.khalil.kkadmin.common.web.ReqBody;
import com.khalil.kkadmin.common.web.RespResult;
import com.khalil.kkadmin.kb.domain.KbDocumentDir;
import com.khalil.kkadmin.kb.dto.KbDmsDocumentDto;
import com.khalil.kkadmin.kb.service.KbDocumentService;
import com.khalil.kkadmin.kb.service.KbDocumentDirService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("kbDocument")
public class KbDocumentController {

    private final KbDocumentService kbDocumentService;
    private final KbDocumentDirService kbDocumentDirService;

    public KbDocumentController(KbDocumentService kbDocumentService, KbDocumentDirService kbDocumentDirService) {
        this.kbDocumentService = kbDocumentService;
        this.kbDocumentDirService = kbDocumentDirService;
    }

    @GetMapping("/dir/tree")
    public RespResult<List<KbDocumentDir>> getKbDocumentDirTree() {
        return RespResult.ok(kbDocumentDirService.getKbDocumentDirTree());
    }

    @PostMapping("/dir")
    public RespResult<KbDocumentDir> addKbDocumentDir(@RequestBody ReqBody<String> reqBody) {
        return RespResult.ok(kbDocumentDirService.create(reqBody.getData()));
    }

    @PutMapping("/dir")
    public RespResult<KbDocumentDir> updateKbDocumentDir(@RequestBody ReqBody<String> reqBody) {
        return RespResult.ok(kbDocumentDirService.update(reqBody.getData()));
    }

    @DeleteMapping("/dir/{id}")
    public void deleteKbDocumentDir(@PathVariable("id") Long id) {
        kbDocumentDirService.deleteById(id);
    }

    @PostMapping("/dir/dragMove")
    public void dragMove(@RequestBody ReqBody<String> reqBody) {
        String data = reqBody.getData();
        JSONObject jsonObject = JSON.parseObject(data);
        Long id = jsonObject.getLong("id");
        Long refId = jsonObject.getLong("refId");
        String dropType = jsonObject.getString("dropType");
        kbDocumentDirService.dragMove(id, refId, dropType);
    }


    @PostMapping("/doc/upload")
    public RespResult<List<KbDmsDocumentDto>> upload(@RequestParam("file") List<MultipartFile> files, @RequestParam("dirId") Long dirId) {
        return RespResult.ok(kbDocumentService.upload(dirId, files));
    }

    @GetMapping("/doc/list")
    public RespResult<List<KbDmsDocumentDto>> getDocList(@RequestParam("dirId") Long dirId) {
        List<KbDmsDocumentDto> kbDocumentList = kbDocumentService.getKbDocumentList(dirId);
        return RespResult.ok(kbDocumentList);
    }

    @PostMapping("/doc/multiDelete")
    public void deleteDoc(@RequestBody ReqBody<String> reqBody) {
        String data = reqBody.getData();
        if (StrUtil.isEmpty(data)) {
            return;
        }
        List<Long> idList = JSON.parseArray(data, Long.class);
        kbDocumentService.deleteByIdList(idList);
    }
}
